
// Copyright (C) Recursion Company. All rights reserved.

#include "stdafx.h"
#include "vol_user_app_info.h"

//-----------------------------------------------------------------------------------------

CVWnd_ShapeBox::CVWnd_ShapeBox ()
{
    m_clrBack = CLR_DEFAULT;
    m_npShapeType = 0;
    m_npLineEffect = 0;
    m_npLineStyle = 1;
    m_npLineWidth = 1;
    m_clrLine = RGB (0, 0, 0);
    m_clrFill = CLR_NONE;
}

BOOL CVWnd_ShapeBox::CreateControl (CVolNativeWndCreateParam* pCreateParam)
{
    pCreateParam->m_dwStyle &= ~WS_TABSTOP;

    return (_CREATE_WND_BY_PARAM (pCreateParam) != NULL);
}

UINT CVWnd_ShapeBox::OnGetDlgCode (LPMSG lpMsg)
{
    return DLGC_STATIC;
}

void CVWnd_ShapeBox::OnPaint (HDC hNotUsed)
{
    CPaintDC dc (m_hWnd);

    CRect rtClipBox;
    dc.GetClipBox (&rtClipBox);
    if (rtClipBox.IsRectEmpty ())
        return;

    const HBRUSH hOldBrush = dc.GetCurrentBrush ();
    const HPEN hOldPen = dc.GetCurrentPen ();

    const INT_P npPenStyle = (m_npLineStyle == 0 ? PS_NULL :
            m_npLineStyle == 1 ? PS_INSIDEFRAME :
            m_npLineStyle == 2 ? PS_DASH :
            m_npLineStyle == 3 ? PS_DOT :
            m_npLineStyle == 4 ? PS_DASHDOT :
            PS_DASHDOTDOT);
    const INT_P npLineWidth = (m_npLineEffect == 0 && m_npLineStyle == 1 ? m_npLineWidth : 1);

    CPen pen1, pen2;
    CRect rt;
    GetClientRect (&rt);

    if (m_npLineEffect == 0)
    {
        pen1.CreatePen ((INT)npPenStyle, (INT)(npLineWidth == 1 ? 0 : npLineWidth), m_clrLine);
    }
    else
    {
        if (m_npLineEffect == 1)
        {
            pen1.CreatePen ((INT)npPenStyle, 0, RGB (255, 255, 255));
            pen2.CreatePen ((INT)npPenStyle, 0, RGB (0, 0, 0));
        }
        else
        {
            ASSERT (m_npLineEffect == 2);
            pen1.CreatePen ((INT)npPenStyle, 0, RGB (0, 0, 0));
            pen2.CreatePen ((INT)npPenStyle, 0, RGB (255, 255, 255));
        }

        if (m_npShapeType == 6)
        {
            rt.top++;
        }
        else if (m_npShapeType == 7)
        {
            rt.left++;
        }
        else
        {
            rt.left++;
            rt.top++;
        }
    }

    CBrush brhBack;
    brhBack.CreateSolidBrush (GetRealBackColor (m_clrBack));
    dc.SelectBrush (brhBack);
    dc.PatBlt (rtClipBox.left, rtClipBox.top, rtClipBox.Width (), rtClipBox.Height (), PATCOPY);

    INT nWidth = rt.Width ();
    INT nHeight = rt.Height ();
    if (m_npShapeType == 1 || m_npShapeType == 3 || m_npShapeType == 5)
    {
        if (nWidth > nHeight)
        {
            rt.left += (nWidth - nHeight) / 2;
            rt.right = rt.left + nHeight;
        }
        else if (nHeight > nWidth)
        {
            rt.top += (nHeight - nWidth) / 2;
            rt.bottom = rt.top + nWidth;
        }
    }

    CBrush brhFill;
    dc.SetBkMode (TRANSPARENT);

    for (INT_P npIndex = 0; npIndex <= 1; npIndex++)
    {
        if (npIndex != 0 && m_npLineEffect == 0)
            break;

        dc.SelectPen (npIndex == 0 ? pen1 : pen2);

        if (npIndex == 1 || m_clrFill == CLR_NONE)
        {
            dc.SelectStockBrush (NULL_BRUSH);
        }
        else
        {
            brhFill.CreateSolidBrush (m_clrFill);
            dc.SelectBrush (brhFill);
        }

        switch (m_npShapeType)
        {
        case 0:
        case 1:
            dc.Rectangle (&rt);
            rt.OffsetRect (-1, -1);
            break;
        case 2:
        case 3:
            dc.Ellipse (&rt);
            rt.OffsetRect (-1, -1);
            break;
        case 4:
        case 5:
            dc.RoundRect (&rt, CPoint (nWidth / 5, nHeight / 5));
            rt.OffsetRect (-1, -1);
            break;
        case 6:
            dc.MoveTo (rt.left, rt.top);
            dc.LineTo (rt.right, rt.top);
            rt.top--;
            break;
        case 7:
            dc.MoveTo (rt.left, rt.top);
            dc.LineTo (rt.left, rt.bottom);
            rt.left--;
            break;
        DEFAULT_FAIL
        }
    }

    dc.SelectPen (hOldPen);
    dc.SelectBrush (hOldBrush );
}

void CVWnd_ShapeBox::SetBackColor (const COLORREF clrBack)
{
    if (m_clrBack != clrBack)
    {
        m_clrBack = clrBack;
        CheckRedraw ();
    }
}

void CVWnd_ShapeBox::SetLineColor (const COLORREF clrLine)
{
    if (m_clrLine != clrLine)
    {
        m_clrLine = clrLine;
        CheckRedraw ();
    }
}

void CVWnd_ShapeBox::SetFillColor (const COLORREF clrFill)
{
    if (m_clrFill != clrFill)
    {
        m_clrFill = clrFill;
        CheckRedraw ();
    }
}

void CVWnd_ShapeBox::SetShapeType (INT_P npShapeType)
{
    npShapeType = CLIP (npShapeType, 0, 7);

    if (m_npShapeType != npShapeType)
    {
        m_npShapeType = npShapeType;
        CheckRedraw ();
    }
}

void CVWnd_ShapeBox::SetLineEffect (INT_P npLineEffect)
{
    npLineEffect = CLIP (npLineEffect, 0, 2);

    if (m_npLineEffect != npLineEffect)
    {
        m_npLineEffect = npLineEffect;
        CheckRedraw ();
    }
}

void CVWnd_ShapeBox::SetLineStyle (INT_P npLineStyle)
{
    npLineStyle = CLIP (npLineStyle, 0, 5);

    if (m_npLineStyle != npLineStyle)
    {
        m_npLineStyle = npLineStyle;
        CheckRedraw ();
    }
}

void CVWnd_ShapeBox::SetLineWidth (INT_P npLineWidth)
{
    npLineWidth = MAX (npLineWidth, 0);

    if (m_npLineWidth != npLineWidth)
    {
        m_npLineWidth = npLineWidth;
        CheckRedraw ();
    }
}
